//
//  NavigationViewController.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/24.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "NavigationViewController.h"

#import "AreaViewController.h"
#import "TripViewController.h"
#import "MineViewController.h"
#import "SaleViewController.h"

@interface NavigationViewController ()<REMenuDelegate>

@property (retain, nonatomic, readwrite) REMenu *menu;

@end

@implementation NavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (REUIKitIsFlatMode()) {
        [self.navigationBar performSelector:@selector(setBarTintColor:) withObject:[UIColor colorWithRed:0/255.0 green:213/255.0 blue:161/255.0 alpha:1]];
        self.navigationBar.tintColor = [UIColor whiteColor];
    } else {
        self.navigationBar.tintColor = [UIColor colorWithRed:0 green:179/255.0 blue:134/255.0 alpha:1];
    }
    
    __typeof (self) __weak weakSelf = self;
    REMenuItem *trip = [[REMenuItem alloc] initWithTitle:@"热门游记"
                                                    subtitle:nil
                                                       image:[UIImage imageNamed:@"Icon_Home"]
                                            highlightedImage:nil
                                                      action:^(REMenuItem *item) {
                                                          TripViewController *controller = [[TripViewController alloc] init];
                                                          [weakSelf setViewControllers:@[controller] animated:NO];
                                                      }];
    
    
    REMenuItem *area = [[REMenuItem alloc] initWithTitle:@"热门景点"
                                                       subtitle:nil
                                                          image:[UIImage imageNamed:@"Icon_Explore"]
                                               highlightedImage:nil
                                                         action:^(REMenuItem *item) {
                                                             AreaViewController *controller = [[AreaViewController alloc] init];
                                                             [weakSelf setViewControllers:@[controller] animated:NO];
                                                         }];
    
    
    REMenuItem *sale = [[REMenuItem alloc] initWithTitle:@"特价旅行"
                                                        subtitle:nil
                                                           image:[UIImage imageNamed:@"Icon_Activity"]
                                                highlightedImage:nil
                                                          action:^(REMenuItem *item) {
                                                              SaleViewController *controller = [[SaleViewController alloc] init];
                                                              [weakSelf setViewControllers:@[controller] animated:NO];
                                                          }];
    
//    sale.badge = @"12";
    
    REMenuItem *mine = [[REMenuItem alloc] initWithTitle:@"我的主页"
                                                          image:[UIImage imageNamed:@"Icon_Profile"]
                                               highlightedImage:nil
                                                         action:^(REMenuItem *item) {
                                                             MineViewController *controller = [[MineViewController alloc] init];
                                                             [weakSelf setViewControllers:@[controller] animated:NO];
                                                         }];
    
    [trip setTextColor:[UIColor cyanColor]];
    [area setTextColor:[UIColor cyanColor]];
    [sale setTextColor:[UIColor cyanColor]];
    [mine setTextColor:[UIColor cyanColor]];

    
    
    trip.tag = 0;
    area.tag = 1;
    sale.tag = 2;
    mine.tag = 3;
    
    self.menu = [[REMenu alloc] initWithItems:@[trip, area, sale, mine]];
    
    // Background view
    //
    //self.menu.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    //self.menu.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //self.menu.backgroundView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.600];
    
    //self.menu.imageAlignment = REMenuImageAlignmentRight;
    //self.menu.closeOnSelection = NO;
    //self.menu.appearsBehindNavigationBar = NO; // Affects only iOS 7
    
    if (!REUIKitIsFlatMode()) {
        self.menu.cornerRadius = 4;
        self.menu.shadowRadius = 4;
        self.menu.shadowColor = [UIColor blackColor];
        self.menu.shadowOffset = CGSizeMake(0, 1);
        self.menu.shadowOpacity = 1;
    }
    
    // Blurred background in iOS 7
    //
    //self.menu.liveBlur = YES;
    //self.menu.liveBlurBackgroundStyle = REMenuLiveBackgroundStyleDark;
    
    self.menu.separatorOffset = CGSizeMake(15.0, 0.0);
    self.menu.imageOffset = CGSizeMake(5, -1);
    self.menu.waitUntilAnimationIsComplete = NO;
    self.menu.badgeLabelConfigurationBlock = ^(UILabel *badgeLabel, REMenuItem *item) {
        badgeLabel.backgroundColor = [UIColor colorWithRed:0 green:179/255.0 blue:134/255.0 alpha:1];
        badgeLabel.layer.borderColor = [UIColor colorWithRed:0.000 green:0.648 blue:0.507 alpha:1.000].CGColor;
    };
    self.menu.delegate = self;
    
    
//    [self.menu setClosePreparationBlock:^{
//        NSLog(@"Menu will close");
//    }];
//    
//    [self.menu setCloseCompletionHandler:^{
//        NSLog(@"Menu did close");
//    }];
    
    
}

- (void)toggleMenu
{
    if (self.menu.isOpen) {
        return [self.menu close];
    }
    [self.menu showFromNavigationController:self];
}

@end
